// pages/withdraw/withdraw.js
const app = getApp();
const utils = require('../../utils/util.js');
const post = utils.post;
const showModal = utils.showModal;
const getCurrentPageUrlWithArgs = utils.getCurrentPageUrlWithArgs;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        z_uid: '',
        m_uid: '',
        list: [],
        tx: '',
        money: '',
        tx_money: '',
        name: '',
        first_tixian: '',
        ifName: true, // false代表显示姓名输入框，true隐藏姓名输入框
        tishi: '首次提现请输入姓名',
        cid: 0,
        placeholder: '请输入您的姓名',
        isPhone: false,
        isPhoneZz: false,
        WithdPhone: '',
        today_tixian: 0,
        time: '获取验证码',
        currentTime: 60,
        ifyzm: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.cash_show();
    },
    cash_show() {
        let that = this;
        that.setData({
            money: '',
            name: '',
            first_tixian: '',
            today_tixian: ''
        })
        let uid = wx.getStorageSync('z_uid');
        that.setData({
            z_uid: wx.getStorageSync('z_uid')
        });
        if (uid) {
            let p = {
                m: 'tixian',
                uid: uid
            }
            post(p).then(res => {
                that.setData({
                    WithdPhone: res.phone
                });
                if (res.phone_check == 0) {
                    var url = encodeURIComponent('/pages/withdraw/withdraw')
                    wx.navigateTo({
                        url: '/pages/phoneJ/phoneJ?org_url=' + url
                    })
                } else {
                    that.setData({
                        money: res.promotion_can_cash,
                        name: res.weixin_name,
                        first_tixian: res.first_tixian,
                        today_tixian: res.today_tixian
                    })
                }

            });
            let p2 = {
                m: 'put_money',
                uid: uid
            }
            post(p2).then(res => {
                //console.log(res)
                that.setData({
                    tx: res.result
                })
            })
        }
    },
    cash_click(event) {
        let that = this;
        var tx_money = event.target.dataset.money;
        if (1 == that.data.today_tixian) {
            showModal(that, {
                content: '每天最多提现一次',
                showCancel: false,
                ok_text: '我知道了',
            });
        } else {
            that.setData({
                tx_money: tx_money,
                isPhone: true,
                isPhoneZz: true,
                ifyzm: true,
            })
        }
    },
    //发送验证码按钮
    yzm_click() {
        let that = this;
        var interval = null;
        let uid = wx.getStorageSync('z_uid');
        let p = {
            m: 'cash_send_code',
            uid: uid
        };
        post(p).then(res => {
            console.log(res);
            if (0 == res.r) {
                wx.showToast({
                    title: "发送成功，请前往手机短信界面查看！",
                    duration: 2000,
                    icon: 'success',
                });
                // 倒数60秒
                var currentTime = that.data.currentTime;
                that.setData({
                    time: currentTime + '秒',
                    ifyzm: false,
                })
                interval = setInterval(function() {
                    that.setData({
                        time: (currentTime - 1) + '秒',
                    })
                    currentTime--;
                    if (currentTime <= 0) {
                        clearInterval(interval)
                        that.setData({
                            time: '重新获取',
                            currentTime: 60,
                            disabled: false,
                            ifyzm: true,
                        })
                    }
                }, 1000);
            } else {
                showModal(that, {
                    content: res.errmsg,
                    showCancel: false,
                    ok_text: '我知道了',
                });
            }
        })
    },
    // 验证码弹窗遮罩层，点击隐藏
    hideModal() {
        let that = this;
        if (that.data.ifyzm) {
            that.setData({
                currentTime: 60,
                isPhone: false,
                isPhoneZz: false
            });
        } else {
            console.log("关不掉！");
        }
    },
    //获取用户输入的验证码
    inputChange(e) {
        let that = this;
        var val = e.detail.value;
        that.setData({
            telphone_code: val
        })
    },
    //验证码窗口确定按钮
    onConfirm() {
        let that = this;
        let uid = wx.getStorageSync('z_uid');
        if (that.data.telphone_code == '' || that.data.telphone_code == undefined) {
            wx.showModal({
                content: '验证码不能为空',
                showCancel: false,
                confirmText: '我知道了'
            });
        } else {
            let p = {
                m: 'cash_check_code',
                uid: uid,
                code: that.data.telphone_code
            };
            post(p).then(res => {
                if (res.r == 0) {
                    that.setData({
                        isPhone: false,
                        isPhoneZz: false
                    });
                    var name = that.data.name;
                    if (name && name.trim().length > 0) {
                        that.setData({
                            ifName: true,
                        });
                        that.cash(that.data.tx_money * 100, name);
                    } else {
                        that.setData({
                            tishi: '首次提现请输入姓名',
                            ifName: false,
                            tx_money: that.data.tx_money
                        });
                    }
                } else {
                    wx.showModal({
                        content: res.errmsg,
                        showCancel: false,
                        confirmText: '我知道了'
                    });
                    // showModal(that, {
                    //     content: res.errmsg,
                    //     showCancel: false,
                    //     ok_text: '我知道了',
                    // });
                }
            })
        }
    },
    cash(money, name) {
        wx.showLoading({
            title: '提现处理中',
        });

        let that = this;
        let p = {
            a: 'cash',
            m: 'tixian',
            uid: that.data.z_uid,
            weixin_name: name,
            money: money
        };

        post(p).then(res => {
            wx.hideLoading();
            if (res.r == 0) {
                showModal(that, {
                    content: res.errmsg,
                    showCancel: false,
                    ok_text: '我知道了',
                });
                that.data.today_tixian = 1;
                that.cash_show();
            } else {
                var cid = res.cid;
                if (cid && cid > 0) {
                    that.setData({
                        cid: cid
                    });
                    var err_code = res.err_code;
                    if (err_code && err_code.length > 0) {
                        if ('NAME_MISMATCH' == err_code) { // 姓名不对
                            that.setData({
                                ifName: false,
                                tishi: '姓名不匹配，请重新输入'
                            });
                        } else if ('NOTENOUGH' == err_code) {
                            showModal(that, {
                                content: res.errmsg,
                                showCancel: false,
                                ok_text: '我知道了',
                            });
                        } else {
                            that.cash_retry(cid, name);
                        }
                    } else {
                        showModal(that, {
                            content: res.errmsg,
                            showCancel: false,
                            ok_text: '我知道了',
                        });
                    }
                } else {
                    showModal(that, {
                        content: res.errmsg,
                        showCancel: false,
                        ok_text: '我知道了',
                    });
                }
            }
        });
    },

    cash_retry(cid, name) {
        wx.showLoading({
            title: '提现处理中，请稍候',
        });

        let that = this;
        let p = {
            uid: that.data.z_uid,
            a: 'retry',
            m: 'tixian',
            cid: cid
        };

        if (name && name.length > 0) {
            p.weixin_name = name;
        }

        post(p).then(res => {
            wx.hideLoading();

            if (res.r == 0) {
                showModal(that, {
                    content: res.errmsg,
                    showCancel: false,
                    ok_text: '我知道了',
                });
                that.data.today_tixian = 1;
                that.cash_show();
            } else {
                var cid = res.cid;
                if (cid && cid > 0) {
                    that.setData({
                        cid: cid
                    });
                    var err_code = res.err_code;
                    if (err_code && err_code.length > 0) {
                        if ('NAME_MISMATCH' == err_code) { // 姓名不对
                            that.setData({
                                ifName: false,
                                tishi: '姓名不匹配，请重新输入'
                            });
                        } else if ('NOTENOUGH' == err_code) {
                            showModal(that, {
                                content: res.errmsg,
                                showCancel: false,
                                ok_text: '我知道了',
                            });
                        } else {
                            that.cash_retry(cid, name);
                        }
                    } else {
                        showModal(that, {
                            content: res.errmsg,
                            showCancel: false,
                            ok_text: '我知道了',
                        });
                    }
                } else {
                    showModal(that, {
                        content: res.errmsg,
                        showCancel: false,
                        ok_text: '我知道了',
                    });
                }
            }
        });
    },

    // 输入名称
    setValue: function(m) {
        let that = this;
        //console.log(m.detail.value)
        that.setData({
            name: m.detail.value
        })
    },

    inputNameFocus: function(m) {
        this.setData({
            placeholder: ''
        });
    },

    inputNameBlur: function(m) {
        this.setData({
            placeholder: '请输入您的姓名'
        });
    },

    // 确定
    confirm: function() {
        let that = this;
        if (0 == that.data.name.length) {
            wx.showToast({
                title: '姓名不能为空',
                icon: 'none',
                duration: 2000,
                mask: true
            });

            return false;
        } else {
            var tx_money = that.data.tx_money;
            var name = that.data.name;
            var cid = that.data.cid;
            if (cid && cid > 0) {
                that.cash_retry(cid, name);
            } else {
                that.cash(tx_money * 100, name);
            }
            that.setData({
                ifName: true
            });
        }
    },
    // 取消
    cancel: function() {
        let that = this;
        that.setData({
            ifName: true
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        app.getLbs();
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },
    submitFormId(e) {
        let formId = e.detail.formId;
        let button = e.detail.target.dataset.name;
        var m_uid = wx.getStorageSync("m_uid");
        let page = getCurrentPageUrlWithArgs();
        let p = {
            m: 'colform',
            uid: m_uid,
            openid: wx.getStorageSync('weapp_openid'),
            formId: formId,
        }
        if (page && page.length > 0) {
            p.page = page;
        }
        if (button && button.length > 0) {
            p.button = button;
        }
        //console.log(p);
        post(p).then(res => {})
    },
})